package tech.veedo.munchkin.controllers.backend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import tech.veedo.munchkin.beans.frontend.QueryParams;
import tech.veedo.munchkin.beans.backend.Beginner;
import tech.veedo.munchkin.constants.Operation;
import tech.veedo.munchkin.services.backend.BeginnerService;
import tech.veedo.munchkin.verifications.backend.BeginnerVerify;

import java.util.List;

@RequestMapping("/munc/beginner")
@RestController
public class BeginnerController {

    private final BeginnerVerify beginnerVerify;

    private final BeginnerService beginnerService;

    public BeginnerController(BeginnerVerify beginnerVerify, BeginnerService beginnerService) {
        this.beginnerVerify = beginnerVerify;
        this.beginnerService = beginnerService;
    }

    @ApiOperation("(批量)保存发起人员")
    @PostMapping("/save")
    public boolean save(@RequestBody List<Beginner> beginners) {
        beginnerVerify.verifyBatch(Operation.ADD, beginners);
        return beginnerService.saveBatch(beginners);
    }

    @ApiOperation("删除发起人员")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return beginnerService.removeById(id);
    }

    @ApiOperation("分页查询发起人")
    @PostMapping("/page")
    public Page<Beginner> page(@RequestBody QueryParams<Beginner> params) {
        beginnerVerify.verify(Operation.QUERY, params.getCondition());
        Page<Beginner> page = new Page<>(params.getCurrent(), params.getSize());
        QueryWrapper<Beginner> wrapper = new QueryWrapper<>();
        wrapper.eq("workflow_id", params.getCondition().getWorkflowID())
                .eq("org_code", params.getCondition().getOrgCode())
                .orderByDesc("create_time");
        return beginnerService.page(page, wrapper);
    }

}
